package com.playball.lottery.customview;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.playball.lottery.R;
import com.playball.lottery.frm.buyhall.BDActivity;

/**
 * 北京单场总进球二级菜单用到的数据适配器
 * 
 * @author Administrator
 * 
 */
/**
 * 总进球gridview用到的数据适配器
 * 
 * @author Administrator
 * 
 */
public class BDZJQGridviewAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<PictureBDZJQ> pictures;
	private int childPos;
	/** 上下文内容 */
	private Context cxt;
	/** 零时存储选择的赛事，由bdActivity来回调 */
	private List<ChildDTO> list_children = new ArrayList<ChildDTO>();

	public BDZJQGridviewAdapter(String[] titles, String[] nums, Context context, List<ChildDTO> appData, int childPosition) {
		super();
		pictures = new ArrayList<PictureBDZJQ>();
		inflater = LayoutInflater.from(context);
		if (nums != null) {
			for (int i = 0; i < nums.length; i++) {
				PictureBDZJQ picture = new PictureBDZJQ(titles[i], nums[i]);
				pictures.add(picture);
			}
		}
		list_children = appData;
		childPos = childPosition;
		cxt = context;
	}

	/**
	 * 获取选择
	 * 
	 * @return
	 */
	// public static List<List<List<String>>> getseltitleslist() {
	// return childData_seltitleslist;
	// }

	@Override
	public int getCount() {
		if (null != pictures) {
			return pictures.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return pictures.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolderBDZJQ viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.buy_jczq_childs_zjqgridview_item, null);
			viewHolder = new ViewHolderBDZJQ();
			// 组件ID
			viewHolder.LinearLayout1 = (RelativeLayout) convertView.findViewById(R.id.jczq_zjq_RelativeLayout1);
			viewHolder.title = (TextView) convertView.findViewById(R.id.jczq_zjq_num);
			viewHolder.num = (TextView) convertView.findViewById(R.id.jczq_zjq_item);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolderBDZJQ) convertView.getTag();
		}
		// 赛事按钮点击与否
		// 获得行数据（模型）
		final ChildDTO info = list_children.get(childPos);
		
		viewHolder.LinearLayout1.setBackgroundResource(getResourceID(position));
		
		// Log.i("zjq",
		// "展示info.getList_gridview_selectstate()= "+groupPos+" ,childPos= "+childPos+","+info.getList_gridview_selectstate().toString());
		if (((String) ((info.getList_zjqgridview_selectstate()).get(position))).equals("false")) {
//			viewHolder.RelativeLayout1.setBackgroundColor(0xffffffff);
			viewHolder.LinearLayout1.setSelected(false);
			// 改变字体颜色
			viewHolder.title.setTextColor(0xff666666);
			viewHolder.num.setTextColor(cxt.getResources().getColor(R.color.ui_new_txt_content_selected));
		} else {
//			viewHolder.RelativeLayout1.setBackgroundColor(0xffe34b56);
			viewHolder.LinearLayout1.setSelected(true);
			// 改变字体颜色
			viewHolder.title.setTextColor(0xffffffff);
			viewHolder.num.setTextColor(0xffffffff);
		}
		final String title = pictures.get(position).getTitle();

		viewHolder.title.setText(title);
		// Log.d("",
		// "pictures.get(position).getNumsp().length()= "+pictures.get(position).getNumsp().length());
		// 0 12.50
		// 4 5.10
		if (pictures.get(position).getNumsp().length() <= 4) {// 5.10 长度4
			viewHolder.num.setText("  " + pictures.get(position).getNumsp());// 2个空格，显示对齐
		} else {
			viewHolder.num.setText(pictures.get(position).getNumsp());
		}

		// 按钮点击事件
		viewHolder.LinearLayout1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// if(list_children.size()!=0){
				// for (ChildDTO child : list_children) {//ChildDTO child :
				// groups.get(groupPos).children
				// String jc_mid = child.getmId();
				// String str_hteam = child.gethTeam();
				// String str_vteam = child.getvTeam();
				// List<String> list_gridview_selectconcent= new
				// ArrayList<String>();
				// list_gridview_selectconcent =
				// child.getList_zjqgridview_selectconcent();
				// // String str_sp = child.getsPzjq();
				// // Log.i("zjq点击", "str_sp=" + str_sp);
				// Log.e("zjq点击", "jc_mid=" + jc_mid);
				// Log.d("zjq点击", "str_hteam=" + str_hteam);
				// Log.d("zjq点击", "str_vteam=" + str_vteam);
				// Log.d("zjq点击", "list_gridview_selectconcent=" +
				// list_gridview_selectconcent.toString());
				// }
				// }
				// Log.e("JczqZJQgridviewAdapter ",
				// "bbbb before list_children list_children.size()= " +
				// list_children.size());
				// 当用户点击后，首先从零时List的取出指定mid的赛事，删除掉
				// 用来装需要删除的元素
				// List<ChildDTO> delList = new ArrayList<ChildDTO>();
				// for (ChildDTO child : list_children) {
				// String jc_mid = child.getmId();
				// if (info.getmId().equals(jc_mid)) {
				// delList.add(child);
				// }
				// }
				// list_children.removeAll(delList);
				// Log.e("JczqZJQgridviewAdapter ",
				// "before list_children list_children.size()= " +
				// list_children.size());
				if (((String) ((info.getList_zjqgridview_selectstate()).get(position))).equals("false")) {
					// 保存按钮选择状态
					List<String> list_grid_btn_lsstate = new ArrayList<String>();
					list_grid_btn_lsstate = info.getList_zjqgridview_selectstate();
					list_grid_btn_lsstate.remove(position);
					list_grid_btn_lsstate.add(position, "true");
					info.setList_zjqgridview_selectstate(list_grid_btn_lsstate);
					
					// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
					List<String> selspslist0 = new ArrayList<String>();
					selspslist0 = info.getList_zjqgridview_selectsp();
					selspslist0.remove(position);
					selspslist0.add(position, pictures.get(position).getNumsp());
					info.setList_zjqgridview_selectsp(selspslist0);

					// 保存按钮内容
					List<String> seltitleslist0 = new ArrayList<String>();
					seltitleslist0 = info.getList_zjqgridview_selectconcent();
					if (seltitleslist0 == null || ((seltitleslist0 != null) && (!seltitleslist0.contains(title)))) {
						seltitleslist0.add(title);
					} else if (seltitleslist0.contains(title)) {
						for (int i = 0; i < seltitleslist0.size(); i++) {
							if (seltitleslist0.get(i).equals(title))
								seltitleslist0.remove(i);
						}
					}
					info.setList_zjqgridview_selectconcent(seltitleslist0);

					// 添加选中项到选中列表中，准备显示到lb tv
					String str0;
					StringBuffer sb = new StringBuffer();
					for (int i = 0; i < seltitleslist0.size(); i++) {
						sb.append(seltitleslist0.get(i) + ","); // ,
					}
					str0 = sb.toString();
//					System.out.println("zjq str0= " + str0);
					info.setSelectZJQCG(str0);
//					// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
//					List<String> selspslist0 = new ArrayList<String>();
//					selspslist0 = info.getList_zjqgridview_selectsp();
//					if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//						selspslist0.add(pictures.get(position).getNumsp());
//					} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//						for (int i = 0; i < selspslist0.size(); i++) {
//							if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//								selspslist0.remove(i);
//						}
//					}
//					info.setList_zjqgridview_selectsp(selspslist0);
				} else {
					// 保存按钮选择状态
					List<String> list_grid_btn_lsstate = new ArrayList<String>();
					list_grid_btn_lsstate = info.getList_zjqgridview_selectstate();
					list_grid_btn_lsstate.remove(position);
					list_grid_btn_lsstate.add(position, "false");
					info.setList_zjqgridview_selectstate(list_grid_btn_lsstate);
					
					// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
					List<String> selspslist0 = new ArrayList<String>();
					selspslist0 = info.getList_zjqgridview_selectsp();
					selspslist0.remove(position);
					selspslist0.add(position, "0");
					info.setList_zjqgridview_selectsp(selspslist0);

					// 保存按钮内容
					List<String> seltitleslist1 = new ArrayList<String>();
					seltitleslist1 = info.getList_zjqgridview_selectconcent();
					if (seltitleslist1 == null || ((seltitleslist1 != null) && (!seltitleslist1.contains(title)))) {
						seltitleslist1.add(title);
					} else if (seltitleslist1.contains(title)) {
						for (int i = 0; i < seltitleslist1.size(); i++) {
							if (seltitleslist1.get(i).equals(title))
								seltitleslist1.remove(i);
						}
					}
					info.setList_zjqgridview_selectconcent(seltitleslist1);

					// 添加选中项到选中列表中，准备显示lb tv
					String str1;
					StringBuffer sb = new StringBuffer();
					for (int i = 0; i < seltitleslist1.size(); i++) {
						sb.append(seltitleslist1.get(i) + ","); // ,
					}
					str1 = sb.toString();
//					System.out.println("zjq str1= " + str1);
					info.setSelectZJQCG(str1);// 1,2
//					// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
//					List<String> selspslist0 = new ArrayList<String>();
//					selspslist0 = info.getList_zjqgridview_selectsp();
//					if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//						selspslist0.add(pictures.get(position).getNumsp());
//					} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//						for (int i = 0; i < selspslist0.size(); i++) {
//							if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//								selspslist0.remove(i);
//						}
//					}
//					info.setList_zjqgridview_selectsp(selspslist0);
				}
				// 将修改后的状态保存到DTO中
				// 存入application List
				list_children.add(info);
				// Log.d("zjq", "点击info.getList_gridview_selectstate()= " +
				// groupPos + " ,childPos= " + childPos + "," +
				// info.getList_zjqgridview_selectstate().toString());
				// for (ChildDTO child : list_children) {//ChildDTO child :
				// groups.get(groupPos).children
				// String jc_mid = child.getmId();
				// String str_hteam = child.gethTeam();
				// String str_vteam = child.getvTeam();
				// List<String> list_gridview_selectconcent= new
				// ArrayList<String>();
				// list_gridview_selectconcent =
				// child.getList_zjqgridview_selectconcent();
				// String selectZJQCG = child.getSelectZJQCG();
				// Log.v("zjq点击", "selectZJQCG=" + selectZJQCG);
				// Log.v("zjq点击", "jc_mid=" + jc_mid);
				// Log.i("zjq点击", "str_hteam=" + str_hteam);
				// Log.i("zjq点击", "str_vteam=" + str_vteam);
				// Log.i("zjq点击", "list_gridview_selectconcent=" +
				// list_gridview_selectconcent.toString());
				// }
				// 删除list重复的数据
				for (int i = 0; i < list_children.size() - 1; i++) {
					for (int j = list_children.size() - 1; j > i; j--) {
						// if
						// (list.get(j).get("number").equals(list.get(i).get("number")))
						// {
						// list.remove(j);
						// }
						String jc_mid1 = list_children.get(i).getmId();
						String selectZJQCG1 = list_children.get(i).getSelectZJQCG();
						String jc_mid2 = list_children.get(j).getmId();
						String selectZJQCG2 = list_children.get(j).getSelectZJQCG();
						if (jc_mid1.equals(jc_mid2) && (selectZJQCG1.equals(selectZJQCG2))) {
							list_children.remove(j);
						}
					}
				}
				// Log.v("JczqZJQgridviewAdapter ",
				// "list_children list_children.size()= " +
				// list_children.size());
				// 回调initMapData这个方法，转存到全局变量里，选号结束
				((BDActivity) cxt).initMapData(list_children, "ZJQ");
				// 重新调用getview，不要在此事件直接设置组件的显示
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

	/*
	 * 根据绘制gridview的position来设置对应item的selector，实现四个顶角不同圆角
	 */
	private int getResourceID(int position) {
		// TODO Auto-generated method stub
		int resourceID = 0;
		switch (position) {
		case 0:
			resourceID = R.drawable.ui_new_gv_bg_top_left_selector;
			break;
		case 3:
			resourceID = R.drawable.ui_new_gv_bg_top_right_selector;
			break;
		case 4:
			resourceID = R.drawable.ui_new_gv_bg_bottom_left_selector;
			break;
		case 7:
			resourceID = R.drawable.ui_new_gv_bg_bottom_right_selector;
			break;
		default:
			resourceID = R.drawable.ui_new_gv_bg_normal_selector;
			break;
		}
		return resourceID;
	}
}

class ViewHolderBDZJQ {
	public TextView title;
	public TextView num;
	public View LinearLayout1;
}

class PictureBDZJQ {
	private String title;
	private String numsp;

	public PictureBDZJQ() {
		super();
	}

	public PictureBDZJQ(String title, String numsp) {
		super();
		this.title = title;
		this.numsp = numsp;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getNumsp() {
		return numsp;
	}

	public void setNumsp(String numsp) {
		this.numsp = numsp;
	}

}
